#include "ustc_comdef.h"
#include "ustc_hif.h"
#include "ustc_api.h"
#include "ustc_cmr_drv.h"

/*===========================================================================
FUNCTION:  ustc_camera_scale

DESCRIPTION: 
	1.	Disable view resize
	2.	Wait until view resize is idle
	3.	Config view resize registers
	4.	Reset view resize
	5.	Enable view resize
	
INPUT PARAMETER:

OUTPUT PARAMETER:  

RETURN VALUE:
===========================================================================*/
void ustc_cmif_scale(RESIZER_T *p_resizer,USTC_U32 winsize)
{
	USTC_U32 rate[2];
	USTC_U32 t_w;
	USTC_U32 temp;
	
//	ustc_io_reset_module(MODULE_CMIF_LOGIC);
	USTC_IO_OUTW(CMR_RESIZE_CTRL,0);
	USTC_IO_OUTW(CMR_FIFO_SELECT,CMR_FIFO_SELECT_INT_WORDS|CMR_FIFO_SIZE_FOR_DMA);
	USTC_IO_OUTW(CMR_SEN_VIDEO_SEL,winsize-0x10001);

	t_w = (p_resizer->t_w+3)&(~3);
	rate[0] = p_resizer->w_w/t_w;
	
	temp = USTC_IO_INW(CMR_RESIZE_CTRL);
	USTC_IO_OUTW(CMR_RESIZE_CTRL,temp | (rate[0]<<4));
	USTC_IO_OUTW(CMR_RESIZE_START_X_POS,p_resizer->x);
	USTC_IO_OUTW(CMR_RESIZE_START_Y_POS,p_resizer->y);
	USTC_IO_OUTW(CMR_RESIZE_END_X_POS,p_resizer->w_w+p_resizer->x-1);
	USTC_IO_OUTW(CMR_RESIZE_END_Y_POS,p_resizer->w_h+p_resizer->y-1);	
	USTC_IO_OUTW(CMR_RESIZE_PIXEL,((USTC_U32)(t_w<<16)|(p_resizer->w_w%t_w)));
	USTC_IO_OUTW(CMR_RESIZE_LINE,((USTC_U32)p_resizer->t_h<<16)|(p_resizer->w_h%p_resizer->t_h));
	

//	USTC_IO_OUTWM(CMR_RESIZE_CTRL,CMR_RESIZE_CTRL_V_PASS|CMR_RESIZE_CTRL_H_PASS,CMR_RESIZE_CTRL_V_PASS|CMR_RESIZE_CTRL_H_PASS);
	USTC_IO_OUTWM(CMR_RESIZE_CTRL,CMR_RESIZE_CTRL_V_PASS|CMR_RESIZE_CTRL_H_PASS,CMR_RESIZE_CTRL_V_PASS);
	
	/* Enable resizer */
	USTC_IO_OUTWM(CMR_RESIZE_CTRL,CMR_RESIZE_CTRL_ENABLE,CMR_RESIZE_CTRL_ENABLE);
}


/*===========================================================================
FUNCTION:  ustc_camera_close

DESCRIPTION: 
	1.	Disable view resize
	2.	Wait until view resize is idle
	3.	Disable sensor interface
	4.	Disable camera clock output enable
	5.	Wait until FIFO is empty
	6.	Disable camera interface (Bit0/REG[0000h])
	
INPUT PARAMETER:

OUTPUT PARAMETER:  

RETURN VALUE:
===========================================================================*/
void ustc_cmif_close(void)
{
	/* Enter logical reset mode */
	
	/* Disable view resize */
	USTC_IO_OUTW(CMR_RESIZE_CTRL,0);

	/* Disable sensor interface */
	USTC_IO_OUTW(CMR_SEN_IF_SEL,0);
}

/*================================================================================
Description: start/close camera. It will perform initialization sensor, 
             sensor clock and resize, and so on. 
             It gets these setting parameters from related host registers.
Input parameter:
Output parameter:
No
Return value:
    Error number
=================================================================================*/
void ustc_cmif_start(USTC_U16 clk_cfg, USTC_U32 wFrameSync)
{
	/* set sensor interface */
	USTC_IO_OUTW(CMR_SEN_IF_SEL, 0);
	
	USTC_IO_OUTW(CMR_SEN_SIGNAL_SEL, clk_cfg);

	USTC_IO_OUTW(CMR_SEN_CCIR656_SEL, wFrameSync);

	USTC_IO_OUTW(CMR_DMA_REQ_MSK, CMR_IF_DMA_NORMAL);

	USTC_IO_OUTW(CMR_INT_MASK, 0);
	USTC_IO_OUTW(CMR_INT_STATU,0x3F);

	if (0)//<pCamera->ccir656_enable)
	{
		USTC_IO_OUTW(CMR_SEN_IF_SEL,CMR_SEN_IF_SEL_SENSOR_IF_ENANLE|CMR_SEN_IF_SEL_SENSOR_CCIR656_ENANLE); 
	}
	else
	{
		USTC_IO_OUTW(CMR_SEN_IF_SEL, CMR_SEN_IF_SEL_SENSOR_IF_ENANLE);
	}
}

/*===========================================================================
 FUNCTION:	cmr_set_image_color_effect
 
 DESCRIPTION: 
	 set collor effect of sensor
	 
 INPUT PARAMETER:
 
 OUTPUT PARAMETER:	
 
 RETURN VALUE:

 ===========================================================================*/
void ustc_cmif_effect(USTC_U32 config)
{
//	g_fw.sys_para.image_effect = config;
	USTC_IO_OUTW(CMR_IMAGE_MODE_SET,config);	
}

USTC_U32 ustc_cmif_int_status()
{
	return USTC_IO_INW(CMR_INT_STATU);
}

void ustc_cmif_int_clear(USTC_U32 int_status)
{
	USTC_IO_OUTW(CMR_INT_STATU, int_status);
}
